package com.youaishujuhui.devops.web.dao.manage;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.StringUtils;
import com.youaishujuhui.devops.web.domain.manage.SysResource;;

@Service
public class SysResourceDao {

	@Resource(name = "nutDao")
    private Dao nutDao;
	
	public int total() {
		Cnd cnd = Cnd.NEW();
		
		return nutDao.count(SysResource.class, cnd);
	}

	public List<SysResource> query(Map<String, Object> param) {
		Cnd cnd = Cnd.NEW();
		
		if (false == StringUtils.isNullOrEmpty((String) param.get("searchName"))) {
			cnd = Cnd.where("name", "like", "%" + param.get("searchName") + "%");
		}

		if (false == StringUtils.isNullOrEmpty((String) param.get("sort"))) {
			String sort = (String) param.get("sort");
			
			if (false == StringUtils.isNullOrEmpty((String) param.get("sort"))) {
				String order = (String) param.get("order");
				
				if (order.equals("asc")) {
					//orderBy = Cnd.orderBy().asc(sort);
					
					cnd.asc(sort);
				} else {
					//orderBy = Cnd.orderBy().desc(sort);
					
					cnd.desc(sort);
				}
			} else {
				//orderBy = Cnd.orderBy().asc(sort);
				
				cnd.asc(sort);
			}
		}
		
		int offset = (int) param.get("offset");
		int limit = (int) param.get("limit");
		Pager pager = new Pager(offset / limit + 1, limit);
		
		
		return nutDao.query(SysResource.class, cnd, pager);
		
	}

	public int countById(SysResource po) {
		return nutDao.count(SysResource.class, Cnd.where("resId", "=", po.getResId()));
	}

	public SysResource insert(SysResource po) {
		return nutDao.insert(po);
	}

	public SysResource findById(Integer id) {
		return nutDao.fetch(SysResource.class, id);
	}

	public int update(SysResource po) {
		return nutDao.update(po);
	}

	public void deleteById(Integer id) {
		nutDao.delete(SysResource.class, id);
	}

	public Set<String> queryAllResourceUris() {
		Set<String> uris = new HashSet<String>();
		List<SysResource> list = nutDao.query(SysResource.class, null);
		if (list != null) {
			for (SysResource sr : list) {
				uris.add(sr.getUri());
			}
		}
		return uris;
	}

	public List<SysResource> resourceTree(Integer roleId) {
		Sql sql = Sqls.create("SELECT a.resId AS id,a.name AS NAME,a.pId AS pId,IF(b.roleId IS NULL,FALSE ,TRUE ) AS checked  FROM t_sys_resource a\r\n" + 
				"		LEFT OUTER JOIN t_role_resource b\r\n" + 
				"		ON a.resId = b.resId AND b.roleId=@roleId\r\n" + 
				"		ORDER BY a.resId");
	    sql.params().set("roleId", roleId);
	    
	    sql.setCallback(new SqlCallback() {
	        public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
	            List<SysResource> list = new LinkedList<SysResource>();
	            while (rs.next()) {
	            	SysResource r = new SysResource();
	            	r.setResId(rs.getInt("id"));
	            	r.setName(rs.getString("name"));
	            	r.setPId(rs.getInt("pId"));
	            	r.setChecked(rs.getBoolean("checked"));
	            	
	                list.add(r);
	            }
	            	
	            return list;
	        }
	    });
	    
	    nutDao.execute(sql);
	    return sql.getList(SysResource.class);
	    // Nutz内置了大量回调, 请查看Sqls.callback的属性
	}
}
